import React from 'react'
import { View } from 'react-native'
import { Lt, Nav, Dialog, Jx, Storage } from "../_utils"
import { Net, NET_NR } from "../_net"
import { eApp, AppPorps, eSort, cHome } from "../home/_home"
import { SearchBar, loadHistory } from "./ApplyHistory"
import { SegProjector } from '../_util';
import { GoodsList } from './ApplyGoodsList';
import { SortBar } from '../home/SortBar';


export class ApplySearch extends React.PureComponent{
    constructor(props){
        super(props);
        const {appIdx, defaultValue, aiSearch} = this.props.navigation.state.params;
        this.defaultValue = defaultValue;
        this.appIdx = appIdx;
        this.aiSearch = aiSearch;
        this.app = eApp.idxs[appIdx];
        this.info = eSort.defaultInfo;

        this.fields = eSort.fields[this.app];
        this.fields[1] = "热度";

        if(aiSearch){
            loadHistory(histories=>{
                if(histories.indexOf(defaultValue) < 0){
                    histories.push(defaultValue);
                    Storage.save(Storage.SEARCH_HISTORY, histories);
                }
            })
        }

        this.onSearch = this.onSearch.bind(this)
        this.onAppIdx = this.onAppIdx.bind(this)
        this.onSort = this.onSort.bind(this)
        this.onLoad = this.onLoad.bind(this)
    }

    static navigationOptions = {
        header:null,
    }
    onSearch(inputText){
        if(this.defaultValue === inputText){
            return;
        }
        this.defaultValue = inputText;
        this.aiSearch = false;
        this.refresh();
    }
    refresh(){
        this.goodslist && this.goodslist.refresh();
    }
    onAppIdx(appIdx){
        this.appIdx = appIdx;
        this.app = eApp.idxs[appIdx];
        this.goodslist && this.goodslist.setAppIdx(appIdx);
        this.refresh();
    }
    onSort(info){
        this.info = info;
        this.refresh();
    }
    onLoad(params){
        if(this.aiSearch && params.page > 0){
            params.add();
            return;
        }
        const type = this.app;
        let obj = {
            type,
            keyword:this.defaultValue,
            ...cHome.getCatCid(type, 0),
            ...cHome.getSort(type, this.info),
            ...params.pagination,
        }
        Net.request("Shop/Search", obj, {params, NET_NR}).then(r=>{
            if(r.data){
                params.add(r.data.data);
            }else{
                params.add();
            }
        });
    }

    render(){
        return <View style = {{flex:1}}>
            <Nav.StateBar />
            <SearchBar defaultValue={this.defaultValue} onSearch = {this.onSearch}/>
            <SegProjector {...AppPorps}
                defaultIdx = {this.appIdx} 
                onChange={this.onAppIdx} 
            />
            <SortBar
                fields = {eSort.fields[this.app]}
                doubles = {eSort.doubles[this.app]}
                onSort={this.onSort}
            />
            <GoodsList
                ref={r=>this.goodslist=r}
                appIdx={this.appIdx}
                onLoad={this.onLoad}
            />
        </View>
    }
}
